/*
Copyright (C) Huawei Technologies Co., Ltd. 2015. All rights reserved.
See LICENSE.txt for this sample's licensing information.
 */
package com.PP.fish.huawei;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.util.Log;

import com.android.huawei.pay.plugin.IHuaweiPay;
import com.android.huawei.pay.plugin.IPayHandler;
import com.android.huawei.pay.plugin.MobileSecurePayHelper;
import com.huawei.gamebox.buoy.sdk.util.DebugConfig;

/**
 * 公共类，提供加载插件和支付接口
 * 
 * @author h00193325
 * 
 */
public class GameBoxUtil
{
    
    // 日志标签
    public static final String TAG = GameBoxUtil.class.getSimpleName();
    
    public static void pay(
        final Activity activity,
        final String price,
        final String productName,
        final String productDesc,
        final String requestId,
        final IPayHandler handler,final String extReserved,final String sign,final String RequestId)
    {

        Map<String, Object> payInfo = new HashMap<String, Object>();
        // 必填字段，不能为null或者""
        payInfo.put(GlobalParam.PayParams.AMOUNT, price);
        // 必填字段，不能为null或者""
        payInfo.put(GlobalParam.PayParams.PRODUCT_NAME, productName);
        // 必填字段，不能为null或者""
        payInfo.put(GlobalParam.PayParams.REQUEST_ID, RequestId);
        // 必填字段，不能为null或者""
        payInfo.put(GlobalParam.PayParams.PRODUCT_DESC, productDesc);
        // 必填字段，不能为null或者""，请填写自己的公司名称
        payInfo.put(GlobalParam.PayParams.USER_NAME, "北京天宏在线网络科技有限公司");
        // 必填字段，不能为null或者""
        payInfo.put(GlobalParam.PayParams.APPLICATION_ID, GlobalParam.APP_ID);
        // 必填字段，不能为null或者""
        payInfo.put(GlobalParam.PayParams.USER_ID, GlobalParam.PAY_ID);
        // 必填字段，不能为null或者""
        payInfo.put(GlobalParam.PayParams.SIGN, sign);
        
        // 必填字段，不能为null或者""，此处写死X6
        payInfo.put(GlobalParam.PayParams.SERVICE_CATALOG, "X6");
        
        // 调试期可打开日志，发布时注释掉
        payInfo.put(GlobalParam.PayParams.SHOW_LOG, true);
        
        // 设置支付界面横竖屏，默认竖屏
        payInfo.put(GlobalParam.PayParams.SCREENT_ORIENT, GlobalParam.PAY_ORI);
        
       payInfo.put(GlobalParam.PayParams.extReserved, extReserved);
        
        DebugConfig.d("startPay", "支付请求参数 : " + payInfo.toString());
        
        IHuaweiPay payHelper = new MobileSecurePayHelper();
        /**
         * 开始支付
         */
        payHelper.startPay(activity, payInfo, handler);
        
    }
    
    /**
     * 支付方法，实现参数签名与调起支付服务
     * 
     * @param activity
     * @param price
     * @param productName
     * @param productDesc
     * @param requestId
     * @param handler
     */
    public static void startPay(
        final Activity activity,
        final String price,
        final String productName,
        final String productDesc,
        final String requestId,
        final IPayHandler handler,final String extReserved,final String sign,final String RequestId)
    {
        // 尚未获取支付的私钥，需要从服务端获取并保存到内存中
        if ("".equals(GlobalParam.PAY_RSA_PRIVATE))
        {
            
        	final String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAjdeJFzQ5fFZDpIKyf5n4UpiV+aommZ3B06SAXX0U+PwsQ68+WnZpFl7YPO1YzJoFE5SuIYOM6Ux1Id0vVZ23zwIDAQABAkA850sp93aneLLatHPIbmg9rt7WsMSaOS68nWmwusCCl7/whM1S1za1gil8kWCmW0KosXG/5m8R/IoKU25C95ohAiEA1nVhMDDC2JGiDYv+MqNJr9ncLjuT8YtHJgeAM4bM/f0CIQCpUTo+IKKzvKkBSXPsV0ZHubOZ54kSGjG9hNyh8L/wuwIfLTyVQ5UFhKkzhagB9qx63p0V1Kq8ijbWyy7J3BSTKQIhAJUob4ynp217d88gbDT6NXmeSG/+nqwJ02PHla47rntdAiEAryI2K6zcq77OCSEIRT4QyuvJKHXfq/be6GrLUH20QUM=";
            GlobalParam.PAY_RSA_PRIVATE = privateKey;
            pay(activity, price, productName, productDesc, requestId, handler,extReserved,sign,RequestId);

        }
        else
        // 如果已经获取过支付私钥，则直接调起支付
        {
        	Log.v(TAG, "已经获得私钥，调用支付");
            pay(activity, price, productName, productDesc, requestId, handler,extReserved,sign,RequestId);
        }
        
    }
}
